package com.cn.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cn.bean.City;
import com.cn.bean.Province;
import com.cn.dao.CityDAO;
import com.cn.dao.ProvinceDAO;
import com.cn.service.CityService;

public class CityServiceImpl implements CityService {
	private ProvinceDAO provinceDao = null;
	private CityDAO cityDao = null;

	public ProvinceDAO getProvinceDao() {
		return provinceDao;
	}

	public void setProvinceDao(ProvinceDAO provinceDao) {
		this.provinceDao = provinceDao;
	}

	public CityDAO getCityDao() {
		return cityDao;
	}

	public void setCityDao(CityDAO cityDao) {
		this.cityDao = cityDao;
	}

	public ArrayList<Map<String, Object>> getCity(String parentId, String level) {
		String hql;
		List<Object> list = null;
		Map<String, Object> map = null;
		ArrayList<Map<String, Object>> rsList = new ArrayList<Map<String, Object>>();
		if (level.equals("province")) {
			hql = "from Province as province";
			list = provinceDao.findByHql(hql);
			Province province;
			for (int i = 0; i < list.size(); i++) {
				map = new HashMap<String, Object>();
				province = (Province) list.get(i);
				map.put("id", province.getId());
				map.put("name", province.getName());
				rsList.add(map);
			}
		} else {
			list = cityDao.findByPId(parentId);
			City city;
			for (int i = 0; i < list.size(); i++) {
				map = new HashMap<String, Object>();
				city = (City) list.get(i);
				map.put("id", city.getIndex());
				map.put("name", city.getName());
				rsList.add(map);
			}
		}
		return rsList;
	}
}
